﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Server
{
	public class AuktionsService
	{
		private object lockobj = new object();


        public delegate void BroadCastEventHandler(string msg);
        public event BroadCastEventHandler BroadCastEvent;

        public event AuktionsService.BroadCastEventHandler ClientInfo;
        
		private int id;
		private List<Auction> auctions;

		public AuktionsService()
		{
			auctions = new List<Auction>();
			this.id = 0;
		}
        /// <summary>
        /// Method for creating a new auction
        /// </summary>
        /// <param name="name">Name of the item</param>
        /// <param name="minimumPrice">The starting price of the item</param>
		public void NewAuction(string name, double minimumPrice)
		{
			lock (lockobj)
			{
				Auction auction = new Auction(++id, name, minimumPrice, this);
				auctions.Add(auction);
				if (BroadCastEvent != null)
					BroadCastEvent("NewAuction;" + id + ";" + name + ";" + minimumPrice + ";" + 0);
			}

		}
        /// <summary>
        /// Method for placing a bid on an item
        /// </summary>
        /// <param name="id">Id on the item you want to bid on</param>
        /// <param name="bid">Your bid</param>
        /// <param name="client">The client sending the bid</param>
        /// <returns></returns>
		public placeBidReturn PlaceBid(int id, double bid, Clienthandler client)
		{
			bool auctionFound = false;
			Auction current = null;
			IEnumerator<Auction> iterator = auctions.GetEnumerator();

			while (!auctionFound && iterator.MoveNext())
			{
				current = (Auction)iterator.Current;

				if (current.Id == id)
				{
					auctionFound = true;
				}
			}

			if (!auctionFound)
				return placeBidReturn.AuctionNotExist;

			if (!current.Active)
				return placeBidReturn.AuctionClosed;

			if (current.HeighestBid != null)
			{
				if (bid <= current.HeighestBid.Bid1)
					return placeBidReturn.BidTooLow;
			}
			else if (current.HeighestBid == null && bid < current.MinimumPrice)
				return placeBidReturn.BidTooLow;

			placeBidReturn returnValue = current.PlaceBid(bid, client);

			if (returnValue == placeBidReturn.BidOk)
				BroadCastEvent("NewBid;" + id + ";" + bid);

			return returnValue;
		}
        /// <summary>
        /// Used for getting the auction list out to the server gui
        /// </summary>
        /// <returns></returns>

        public IEnumerator<Auction> GetAuctions()
        {
            return auctions.GetEnumerator();
        }
        /// <summary>
        /// Method used for broadcasting
        /// </summary>
        /// <param name="msg"></param>
        public void Broadcast(string msg)
        {
            if (BroadCastEvent != null)
            {
                BroadCastEvent(msg);
            }
        }

        public void ClientInformation(string msg)
        {
            ClientInfo(msg);
        }
    }
}
